//
//  WebViewController.m
//  Cachalot
//
//  Created by Adam Cue on 6/25/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "WebViewController.h"

static UIImage * _backButtonImage = nil;
static UIImage * _backTouchButtonImage = nil;
static UIImage * _forwardButtonImage = nil;
static UIImage * _forwardTouchButtonImage = nil;
static UIImage * _stopButtonImage = nil;
static UIImage * _stopTouchButtonImage;
static UIImage * _actionButtonImage = nil;
static UIImage * _actionTouchButtonImage = nil;
static UIImage * _reloadButtonImage;
static UIImage * _reloadTouchButtonImage;

@interface WebViewController (Private)
#pragma mark SetupMethods
- (void) setupStaticImages;

#pragma mark UserActions
- (void) stop;
- (void) refresh;
@end

@implementation WebViewController

@synthesize url = _url;
@synthesize webView = _webView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil address:(NSString *)address
{
   if (!(self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) return self;
   
   self.url = address;
   [self setupStaticImages];
   
   return self;
}

- (void)viewDidLoad
{
   [super viewDidLoad];
   _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0.0, 44.0, 1024.0, 660.0)];
   self.webView.scalesPageToFit = YES;
   self.webView.delegate = self;
   [self.view addSubview:self.webView];
   [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.url]]];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
   return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

- (void)dealloc {
   self.url = nil;
   self.webView = nil;
   
   [super dealloc];
}

#pragma mark SetupMethods
- (void) setupStaticImages {
	if(!_backButtonImage) _backButtonImage = [[UIImage imageNamed:@"left.png"] retain];
	if(!_backTouchButtonImage) _backTouchButtonImage = [[UIImage imageNamed:@"left-touch.png"] retain];
	if(!_forwardButtonImage) _forwardButtonImage = [[UIImage imageNamed:@"right.png"] retain];
	if(!_forwardTouchButtonImage) _forwardTouchButtonImage = [[UIImage imageNamed:@"right-touch.png"] retain];
	if(!_stopButtonImage) _stopButtonImage = [[UIImage imageNamed:@"stop.png"] retain];
	if(!_stopTouchButtonImage) _stopTouchButtonImage = [[UIImage imageNamed:@"stop-touch.png"] retain];
	if(!_actionButtonImage) _actionButtonImage = [[UIImage imageNamed:@"export.png"] retain];
	if(!_actionTouchButtonImage) _actionTouchButtonImage = [[UIImage imageNamed:@"export-touch.png"] retain];
	if(!_reloadButtonImage) _reloadButtonImage = [[UIImage imageNamed:@"reload.png"] retain];
	if(!_reloadTouchButtonImage) _reloadTouchButtonImage = [[UIImage imageNamed:@"reload-touch.png"] retain];
}

#pragma mark UIWebViewDelegate
- (void)webViewDidStartLoad:(UIWebView *)webView{
	
	//show the spinner on the navBar
	UIActivityIndicatorView * spinner = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease];
   [spinner startAnimating];
	UIBarButtonItem * spinnerItem = [[[UIBarButtonItem alloc] initWithCustomView:spinner] autorelease];
	NSMutableArray * items = [[_topBar items] mutableCopy];
   [items addObject:spinnerItem];
   [_topBar setItems:items];
   [items release];
	
	//set refresh button to stop.
   [_stopRefreshButton setImage:_stopButtonImage forState:UIControlStateNormal];
   [_stopRefreshButton setImage:_stopTouchButtonImage forState:UIControlStateHighlighted];
   [_stopRefreshButton removeTarget:self action:@selector(refresh) forControlEvents:UIControlEventTouchUpInside];
   [_stopRefreshButton addTarget:self action:@selector(stop) forControlEvents:UIControlEventTouchUpInside];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{
	
	//hide the spinner on the navBar
   NSMutableArray * items = [[_topBar items] mutableCopy];
   [items removeLastObject];
   [_topBar setItems:items];
   [items release];
	
	//check viability of back and forward buttons, and set stop button to refresh.
	self.url = [[webView.request URL] absoluteString];
   _backButton.enabled = [webView canGoBack];
   _forwardButton.enabled = [webView canGoForward];
   [_stopRefreshButton setImage:_reloadButtonImage forState:UIControlStateNormal];
   [_stopRefreshButton setImage:_reloadTouchButtonImage forState:UIControlStateHighlighted];
   [_stopRefreshButton removeTarget:self action:@selector(stop) forControlEvents:UIControlEventTouchUpInside];
   [_stopRefreshButton addTarget:self action:@selector(refresh) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark UserActions
- (IBAction) backButtonPressed:(id)sender {
   //Go back and check viability of back and forward buttons.
   [self.webView goBack];
   self.url = [[self.webView.request URL] absoluteString];
   _backButton.enabled = [self.webView canGoBack];
   _forwardButton.enabled = [self.webView canGoForward];
}

- (IBAction) forwardButtonPressed:(id)sender {
   //Go forward and check viability of back and forward buttons.
   [self.webView goForward];
   self.url = [[self.webView.request URL] absoluteString];
   _backButton.enabled = [self.webView canGoBack];
   _forwardButton.enabled = [self.webView canGoForward];
}

- (void)stop{
	
	[self.webView stopLoading];
	
	//hide the spinner on the navBar
   NSMutableArray * items = [[_topBar items] mutableCopy];
   [items removeLastObject];
   [_topBar setItems:items];
   [items release];
	
	//Set stop button to refresh.
   [_stopRefreshButton setImage:_reloadButtonImage forState:UIControlStateNormal];
   [_stopRefreshButton setImage:_reloadTouchButtonImage forState:UIControlStateHighlighted];
   [_stopRefreshButton removeTarget:self action:@selector(stop) forControlEvents:UIControlEventTouchUpInside];
   [_stopRefreshButton addTarget:self action:@selector(refresh) forControlEvents:UIControlEventTouchUpInside];
}

- (void)refresh{
   //Refresh web view.
   [self.webView reload];
}

- (IBAction) actionButtonPressed:(id)sender {
   //Bring up action sheet for opening link in Safari and copying link.
   UIActionSheet * actionSheet = [[[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel"
                                                destructiveButtonTitle:nil otherButtonTitles:@"Open in Safari", @"Copy Link", nil] autorelease];
   actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
//   CGPoint origin = [_actionButton convertPoint:_actionButton.origin toView:self.view];
   [actionSheet showFromBarButtonItem:_actionBarButtonItem animated:YES];
}

- (IBAction) doneButtonPressed:(id)sender {
   [self dismissModalViewControllerAnimated:YES];
}

#pragma mark UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
   //If they clicked open link in Safari then open the link in Safari.
   if(buttonIndex == 0){
      [[CachalotApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"s%@", self.url]]];
   }
   else if(buttonIndex == 1){
      [[UIPasteboard generalPasteboard] setString:self.url];
   }
}

@end
